package com.fastadmin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fastadmin.mapper.impl.ResourceMapper;
import com.fastadmin.pojo.FastParam;
import com.fastadmin.pojo.Resource;
import com.google.common.collect.ImmutableMap;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequiredArgsConstructor
public class ResourceController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ResourceController.class);

    private final ResourceMapper resourceMapper;

    @GetMapping("/sys/menu/index")
    public String index() {
        return "sys/menu/index";
    }

    @GetMapping("/sys/menu/add")
    public String add(Model model) {
        //查询出所有的父菜单
        List<Resource> resources = resourceMapper.findTypeResource(CollUtil.toList(0, 1));
        model.addAttribute("resources", resources);
        return "sys/menu/add";
    }

    @GetMapping("/sys/menu/edit")
    public String edit(String ids, Model model) {
        Resource resource = resourceMapper.findId(ids);
        List<Resource> resources = resourceMapper.findTypeResource(CollUtil.toList(0, 1));
        model.addAttribute("resources", resources);
        model.addAttribute("entity", resource);
        return "sys/menu/edit";
    }


    @RequestMapping("/sys/menu/data")
    @ResponseBody
    public Object data(FastParam fastParam) {
        PageInfo<Resource> pageInfo = resourceMapper.findByFastParam(fastParam);
        for (Resource resource : pageInfo.list) {
            if (resource.getOnemenu() == 0) {
            } else if (resource.getOnemenu() == 1) {
                resource.setTitle("├" + resource.getTitle());
            }else if (resource.getOnemenu() == 2){
                resource.setTitle("│=" + resource.getTitle());
            }

        }
        return ImmutableMap.of("rows", pageInfo.list, "total", pageInfo.total);
    }
    @RequestMapping(value = "/sys/menu/save")
    @ResponseBody
    public Object save(Resource resource) {
        if (resource.getOnemenu() == 1) {
            resource.setPid(0L);
        }
        if (ObjectUtil.isNotNull(resource.getId())) {
            //修改
            resourceMapper.updateByPrimaryKey(resource);
        } else {
            //新增
            resourceMapper.save(resource);
        }
        return buildJson(1, "OK");
    }
    @RequestMapping("/sys/menu/del")
    @ResponseBody
    public Object del(String ids) {

        resourceMapper.deleteByIds(ids);
        return buildJson(1, "ok");
    }
}
